package my.jwds.lcp40;

import java.util.ArrayList;
import java.util.List;

public class MaxmiumScore {

    public static void main(String[] args) {
        MaxmiumScore obj = new MaxmiumScore();
        System.out.println(obj.maxmiumScore(new int[]{1,2,8,9},3));
    }

    public int maxmiumScore(int[] cards, int cnt) {
        int [] nums = new int[1001];
        for (int card : cards) {
            nums[card] ++;
        }
        List<Integer> odds = new ArrayList<>();
        List<Integer> evens=  new ArrayList<>();
        odds.add(0);
        evens.add(0);
        for (int i = 1000; i > 0; i--) {
            List<Integer> item;
            if (i%2 == 0){
               item = evens;
            }else{
               item = odds;
            }
            while(nums[i] != 0){
                item.add(item.get(item.size()-1)+i);
                nums[i]--;
            }

        }

        int res = 0;
        for (int i = 0; i < odds.size(); i+=2) {
            if (cnt - i >=0 && evens.size() > cnt - i){
                res = Math.max(res,odds.get(i)+evens.get(cnt-i));
            }
        }
        return res;
    }

}
